#!/usr/bin/env zsh

set -x

# initialization
yabai -m signal --add event=dock_did_restart action="sudo yabai --load-sa"
sudo yabai --load-sa

source ~/.config/yabai/initialization.sh

# ====
# Signals
# ====

gaps_top="30"
gaps_bottom="10"
gaps_left="10"
gaps_right="10"
gaps_inner="10"

# Initialize the temp folder when adding or removing displays
yabai -m signal --add event=display_added action="~/.config/yabai/config/yabai/initialization.sh"
yabai -m signal --add event=display_removed action="~/.config/yabai/config/yabai/initialization.sh"
yabai -m signal --add event=window_focused action="~/.config/yabai/config/yabai/change-window-border-color-when-window-focused.sh"
yabai -m signal --add event=window_destroyed action="~/.config/yabai/config/yabai/auto-focus-to-new-one-when-window-destroyed.sh 2> /dev/null"
yabai -m signal --add event=application_hidden action="~/.config/yabai/config/yabai/auto-focus-to-new-one-when-window-destroyed.sh 2> /dev/null"
yabai -m signal --add event=window_minimized action="~/.config/yabai/config/yabai/auto-focus-to-new-one-when-window-destroyed.sh 2> /dev/null"

# ====
# Settings
# ====

yabai -m config mouse_follows_focus          off
yabai -m config focus_follows_mouse          off
yabai -m config window_placement             second_child
yabai -m config window_topmost               off
yabai -m config window_shadow                off

yabai -m config window_border                off
yabai -m config window_border_placement      inset
yabai -m config window_border_blur           off
yabai -m config window_border_hidpi          off
yabai -m config window_border_width          2
yabai -m config window_border_radius         12
yabai -m config active_window_border_topmost off
yabai -m config active_window_border_color   0xfcFFEE58
yabai -m config normal_window_border_color   0x00ffffff
yabai -m config insert_window_border_color   0xffd75f5f

yabai -m config window_opacity               off
yabai -m config window_opacity_duration      96
yabai -m config active_window_opacity        1.0
yabai -m config normal_window_opacity        1.0
yabai -m config split_ratio                  0.50
yabai -m config auto_balance                 off
yabai -m config mouse_modifier               fn
yabai -m config mouse_action1                move
yabai -m config mouse_action2                resize

yabai -m config layout                       bsp
yabai -m config top_padding                  2
yabai -m config bottom_padding               46
yabai -m config left_padding                 2
yabai -m config right_padding                2
yabai -m config window_gap                   6

# 3rd part border
borders active_color=0xfcfeee58 inactive_color=0x00494d64 width=5.0 &

# animation
#yabai -m config window_animation_duration    0.30

# ====
# Rules
# ====

otherApp='^(IINA|访达|Calendar|日历|System Preferences|系统设置|Mail|微信|WeChatc|QQ|印象笔记|灵犀|网易云音乐|Tencent Lemon|企业微信|Todoist|Input Source Pro|Enpass|预览|scrcpy|文本编辑|Kap|Shottr|Listen1|NeatDownloadManager|UVR|CleanMyMac X|归档实用工具)$'
yabai -m rule --add app="${otherApp}" manage=off

## specific rule

osascript -e 'tell application id "tracesOf.Uebersicht" to refresh'

yabai -m rule --add app="^IntelliJ IDEA$" manage=off
yabai -m rule --add app="^IntelliJ IDEA$" title="( – )" manage=on

echo "yabai configuration loaded.."
